package com.primestone.cim.newdata.service;

import java.util.List;

import javax.xml.ws.Holder;

import com.primestone.cim.newdata.FaultMessage;
import com.primestone.cim.newdata.GetNewDataRequestType;
import com.primestone.cim.newdata.HeaderType;
import com.primestone.cim.newdata.MessageProperty;
import com.primestone.cim.newdata.NewDataFaultMessageType;
import com.primestone.cim.newdata.ReplyType;
import com.primestone.cim.util.CollectionUtil;
import com.primestone.cim.util.StringUtil;
import com.primestone.cim.util.enums.MessageEnum;
import com.primestone.cim.util.enums.ResultEnum;

/**
 * @author cesar.mateus
 *
 */
public class Completer {


		/**
	 * 
	 * @param message
	 * @return
	 */
	public static FaultMessage composeFault(String message) {
		NewDataFaultMessageType meterReadingsFaultMessage = new NewDataFaultMessageType();
		ReplyType replyType = new ReplyType();
		replyType.setResult(ResultEnum.FAILED.getName());
		meterReadingsFaultMessage.setReply(replyType);			
		return new FaultMessage(message, meterReadingsFaultMessage);
	}

	/**
	 * 
	 * @param header
	 * @return
	 */
	public static FaultMessage validateHeader(Holder<HeaderType> header) {
		if (header == null || header.value == null) {
			return Completer.composeFault(MessageEnum.INCOMPLETE_HEADER_INFO.getDescription());
		}

		if ( header.value.getUser() == null || !StringUtil.safe(header.value.getUser().getUserID()) || 
				!CollectionUtil.safe(header.value.getProperty()) || !existPwd(header.value.getProperty()) ) {
			return Completer.composeFault(MessageEnum.INCOMPLETE_USER_INFO.getDescription());
		}
		return null;
	}

	/**
	 * 
	 * @param properties
	 * @return
	 */
	public static boolean existPwd( List<MessageProperty> properties) {
		boolean existPwd = false;
		for (MessageProperty messageProperty : properties) {
			if (StringUtil.safe(messageProperty.getName()) && StringUtil.safe(messageProperty.getValue()) 
					&& messageProperty.getName().equals(StringUtil.PWD_PROP_NAME) ) {
				existPwd = true;
				break;
			}
		}
		return existPwd;
	}
	
	/**
	 * 
	 * @param properties
	 * @return
	 */
	public static String extractPwd(List<MessageProperty> properties) {
		if (CollectionUtil.safe(properties)) {
			for (MessageProperty messageProperty : properties) {
				if (StringUtil.safe(messageProperty.getName()) && StringUtil.safe(messageProperty.getValue()) 
						&& messageProperty.getName().equals(StringUtil.PWD_PROP_NAME) ) {
					return messageProperty.getValue();
				}
			}	
		}
		
		return null;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	public static FaultMessage validateRequest(GetNewDataRequestType request) {
		FaultMessage faultMessage = null;
		if (request == null) {
			faultMessage = Completer.composeFault(MessageEnum.INCOMPLETE_REQUEST_INFO.getDescription());			
		} else {
			Byte wasError = 0;
			if (!CollectionUtil.safe(request.getOption())) {
				wasError++;				
			}
			if (!CollectionUtil.safe(request.getID())) {
				wasError++;				
			}
			if (!CollectionUtil.safe(request.getAny())) {
				wasError++;				
			}
			
			if (wasError.equals(3)) {
				faultMessage = Completer.composeFault(MessageEnum.INCOMPLETE_REQUEST_OPTIONS_ID_ANY.getDescription());
			}
		}

		return faultMessage;
	}

	
}
